<?php
/**
 * Created by PhpStorm.
 * User: bigniu
 * Date: 2019-12-17
 * Time: 15:30:59
 */

namespace app\admin\controller;


use Phinx\Util\Util;
use think\Controller;
use app\admin\model\Shops as shopmodel;

/**
 * TODO 待注释
 * Class VipShop
 * @package app\admin\controller
 */
class Shops extends Admin
{
    public function getList()
    {

        $page = input("page/i", 1) <= 1 ? 1 : input("page/i", 1);
        $pageSize = input("pageSize/i", 10) <= 10 ? 10 : input("pageSize/i", 10);
        $whereOr = [];
        $where = [];
        $name = input('name');
        if ($name) {
            $whereOr = [
                'title' => ['like', "%{$name}%"]
            ];
        }
        $type = input('type/i');
        if ($type) {
            $where['type'] = $type;
        }
        $uid = input('uid/i');
        if ($uid) {
            $where['uid'] = $uid;
        }
        $state = input('state/i');
        if ($state != null) {
            $where['state'] = $state;
        }
        $videoList = Db("shops v")
            ->join('shop_type t', 'v.type_id = t.id', 'left')
            ->join('shop_type t1', 't1.id = t.pid', 'left')
            ->whereOr($whereOr)
            ->where($where)
            ->page($page, $pageSize)
            ->field([
                'v.id',
                'v.name',
                'v.price',
                'v.type_id',
                'v.rep_num',
                'v.cover',
                'v.slider',
                'v.content',
                'v.status',
                'v.sort',
                'v.created_at',
                't.name type_name',
                't1.name ptype_name',
                't1.id pid',
            ])
            ->order("v.id desc")
            ->select();

        $count = Db("video")
            ->whereOr($whereOr)
            ->where($where)
            ->count();
        return success("获取成功", $videoList, $page, $count);

        $list = shopmodel::with('shopType')->paginate();//-> Db("shops")->paginate();

        return success("获取成功", $list->toArray());
    }

    public function add()
    {
        $title = input("title");
        $price = input("price");
        $state = input("state/i");
        $desc = input("desc");
        $time = input('time');
        $data = [
            'title' => $title,
            'price' => $price,
            'state' => $state,
            'desc' => $desc,
            'state' => 1,
            'time' => $time,
            'create_time' => TIME
        ];
        $res = Db("vip_shop")->insert($data);
        if ($res) {
            return success("添加成功");
        }
        return error("添加失败");
    }

    public function delete()
    {
        $ids = input('ids/a');
        u_log("删除VIP商品(" . implode($ids, ',') . "成功");
        Db("vip_shop")->whereIn('id', $ids)->delete();
        return success("删除成功");
    }

    public function update()
    {
        $id = input("id/i");
        $title = input("title");
        $price = input("price");
        $state = input("state/i");
        $desc = input("desc");
        $time = input('time');
        $data = [
            'title' => $title,
            'price' => $price,
            'state' => $state,
            'desc' => $desc,
            'time' => $time
        ];
        $res = Db("vip_shop")->where(['id' => $id])->update($data);
        if ($res) {
            return success("修改成功");
        }
        return error("修改失败");
    }
}
